home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / tests / ctype / ctype.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-17  |  3.7 KB  |  163 lines

  1. /* 
  2.  * ctype.c --
  3.  *
  4.  *    This file contains a program that exercises the ctype
  5.  *    library facilities.  Invoke it with no parameters;  it
  6.  *    will print messages on stderr for any problems it detects
  7.  *    with the string procedures.
  8.  *
  9.  * Copyright 1988 Regents of the University of California
  10.  * Permission to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose and without
  12.  * fee is hereby granted, provided that the above copyright
  13.  * notice appear in all copies.  The University of California
  14.  * makes no representations about the suitability of this
  15.  * software for any purpose.  It is provided "as is" without
  16.  * express or implied warranty.
  17.  */
  18.  
  19. #ifndef lint
  20. static char rcsid[] = "$Header: ctype.c,v 1.1 88/04/27 18:02:43 ouster Exp $ SPRITE (Berkeley)";
  21. #endif not lint
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25.  
  26. char *alnum =    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  27. char *alpha =    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  28. char *cntrl =    "\1\2\3\4\5\6\7\10\11\12\13\14\15\16\17\20\21\22\23\24\25\26\27\30\31\32\33\34\35\36\37\177";
  29. char *digit =    "1234567890";
  30. char *graph =    "!\42#$%&'()*+`,-./:;<=>?@[]\\_^{}|~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  31. char *lower =    "abcdefghijklmnopqrstuvwxyz";
  32. char *print =    " !\42#$%&'()*+`,-./:;<=>?@[]\\_^{}|~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  33. char *punct =    "!\42#$%&'()*+`,-./:;<=>?@[]\\_^{}|~";
  34. char *space =    " \11\12\13\14\15";
  35. char *upper =    "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  36. char *xdigit =    "01234567890ABCDEFabcdef";
  37.  
  38. #define check(string, macro, msg)            \
  39.     setflags(string);                    \
  40.     checkflags(macro, msg);
  41.  
  42. #define checkflags(macro,msg)                    \
  43.     for (i = -1; i < 256; i++) {                \
  44.     if (macro(i) ? !flags[i+1] : flags[i+1]) {        \
  45.         fprintf(stderr, "%s at character 0x%02x\n", msg, i);    \
  46.         result = 1;                        \
  47.     }                            \
  48.     }
  49.  
  50. char flags[256];
  51.  
  52. void
  53. setflags(string)
  54.     char *string;
  55. {
  56.     int i;
  57.  
  58.     for (i = 0; i < 257; i++) {
  59.     flags[i] = 0;
  60.     }
  61.     for ( ; *string != 0; string++) {
  62.     flags[1+*string] = 1;
  63.     }
  64. }
  65.  
  66. main()
  67. {
  68.     int result = 0;
  69.     int i;
  70.  
  71.     /*
  72.      * isalnum
  73.      */
  74.  
  75.     check(alnum, isalnum, "isalnum error 1");
  76. #undef isalnum;
  77.     check(alnum, isalnum, "isalnum error 2");
  78.  
  79.     /*
  80.      * isalpha
  81.      */
  82.  
  83.     check(alpha, isalpha, "isalpha error 1");
  84. #undef isalpha;
  85.     check(alpha, isalpha, "isalpha error 2");
  86.  
  87.     /*
  88.      * iscontrl
  89.      */
  90.  
  91.     setflags(cntrl);
  92.     flags[1] = 1;
  93.     checkflags(iscntrl, "iscntrl error 1");
  94. #undef iscntrl
  95.     checkflags(iscntrl, "iscntrl error 2");
  96.  
  97.     /*
  98.      * isdigit
  99.      */
  100.  
  101.     check(digit, isdigit, "isdigit error 1");
  102. #undef isdigit;
  103.     check(digit, isdigit, "isdigit error 2");
  104.  
  105.     /*
  106.      * isgraph
  107.      */
  108.  
  109.     check(graph, isgraph, "isgraph error 1");
  110. #undef isgraph;
  111.     check(graph, isgraph, "isgraph error 2");
  112.  
  113.     /*
  114.      * islower
  115.      */
  116.  
  117.     check(lower, islower, "islower error 1");
  118. #undef islower;
  119.     check(lower, islower, "islower error 2");
  120.  
  121.     /*
  122.      * isprint
  123.      */
  124.  
  125.     check(print, isprint, "isprint error 1");
  126. #undef isprint;
  127.     check(print, isprint, "isprint error 2");
  128.  
  129.     /*
  130.      * ispunct
  131.      */
  132.  
  133.     check(punct, ispunct, "ispunct error 1");
  134. #undef ispunct;
  135.     check(punct, ispunct, "ispunct error 2");
  136.  
  137.     /*
  138.      * isspace
  139.      */
  140.  
  141.     check(space, isspace, "isspace error 1");
  142. #undef isspace;
  143.     check(space, isspace, "isspace error 2");
  144.  
  145.     /*
  146.      * isupper
  147.      */
  148.  
  149.     check(upper, isupper, "isupper error 1");
  150. #undef isupper;
  151.     check(upper, isupper, "isupper error 2");
  152.  
  153.     /*
  154.      * isxdigit
  155.      */
  156.  
  157.     check(xdigit, isxdigit, "isxdigit error 1");
  158. #undef isxdigit;
  159.     check(xdigit, isxdigit, "isxdigit error 2");
  160.  
  161.     return result;
  162. }
  163.